package com.aco.party.service.system;

import com.aco.party.dao.custom.PeopleDao;
import com.aco.party.dao.custom.UserDao;
import com.aco.party.enums.VisitTypeEnum;
import com.aco.party.model.po.People;
import com.aco.party.model.po.User;
import com.aco.party.model.vo.PeopleParam;
import com.aco.party.model.vo.PeopleVo;
import com.aco.party.model.vo.VisitParam;
import com.aco.party.model.vo.common.PageParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by flying on 3/10/2016 0010.
 */
@Service
public class PeopleService {
    @Resource
    private PeopleDao peopleDao;

    @Resource
    private UserDao userDao;

    @Resource
    private VisitService visitService;
    public List<PeopleVo> getByPage(PageParam pageParam,PeopleParam param){
        List<PeopleVo> list = new ArrayList<>();
        List<People> peoples = peopleDao.getList(pageParam,param);
        PeopleVo vo;
        if(peoples != null){
            for (People people : peoples){
                vo = new PeopleVo();
                BeanUtils.copyProperties(people, vo);
                VisitParam visitParam = new VisitParam();
                visitParam.setType(VisitTypeEnum.大咖.getValue());
                visitParam.setTargetId(vo.getId());
                vo.setViews(visitService.getCountByParam(visitParam).longValue());
                if(vo.getCreator() != null && vo.getCreator() != 0){
                    User user = userDao.selectByPrimaryKey(vo.getCreator());
                    if(user != null) {
                        vo.setCreatorStr(user.getNickName());
                    }
                }
                list.add(vo);
            }
        }
        return list;
    }

    public Integer getTotal(PeopleParam param){
        return peopleDao.getTotal(param);
    }

    public void update(People people){
        people.setUpdateTime(new Date());
        peopleDao.updateByPrimaryKeySelective(people);
    }

    public void add(People people){
        people.setCreateTime(new Date());
        peopleDao.insertSelective(people);
    }

    public void delete(Long id){
        peopleDao.deleteByPrimaryKey(id);
    }

    public People getByPrimaryKey(Long id){
        return peopleDao.selectByPrimaryKey(id);
    }

    public PeopleVo getVoById(Long id) {
        PeopleVo vo = new PeopleVo();
        People people = peopleDao.selectByPrimaryKey(id);
        BeanUtils.copyProperties(people, vo);
        return vo;
    }
}
